package com.py.credit.service.excel.read.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.dao.RuleCheckingDao;
import com.py.credit.entity.*;
import com.py.credit.entity.common.InspectRule;
import com.py.credit.service.TableService;
import com.py.credit.service.excel.read.service.InspectDataStyleService;
import com.py.credit.service.excel.read.service.RuleCheckService;

import cn.hutool.json.JSONObject;

import com.py.credit.service.excel.read.InspectHandles;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能描述: 数据采集模块数据校验
 *
 * @author YaoS
 * @date 19/3/29 11:12
 */
@Service("default")
public class InspectDataStyleServiceImpl implements InspectDataStyleService {


    @Autowired
    private RuleCheckingDao ruleCheckingDao;

    @Autowired
    private RuleCheckService ruleCheckService;

    @Autowired
    private TableService tableService;

    /**
     * 重复性校验：先生成MD5,然后删除
     *
     * @param listDataMap 数据的map
     * @param table       要上传的表
     * @param columns     上传字段
     * @param useMemory   是否使用临时表 true是 false否
     * @return 校验的结果
     */
    @Override
    public Map<String, Object> duplicationCheck(List<Map<String, Object>> listDataMap, Table table,
                                                List<Column> columns, boolean useMemory, boolean checkRepeat) {
        StringBuilder duplicationMess = new StringBuilder(200);
        int duplicationCount = 0;
        int i = 0;
        Set<String> set = new HashSet<>(listDataMap.size());
        List<Map<String, Object>> listDataMap2 = (List<Map<String, Object>>) JSONArray.parse(JSONArray.toJSONString(listDataMap));
        for(Map<String, Object> data:listDataMap2){
            data.put("errorMess","");
        }
        String emptyMd5 = InspectHandles.calcMD5byColumn(columns);
        for (Iterator<Map<String, Object>> it = listDataMap.listIterator(); it.hasNext(); ) {
            Map<String, Object> next = it.next();
            i++;
            String index = (null != next.get("rowIndex") ? String.valueOf(next.get("rowIndex")) : String.valueOf(i));
            //MD5计算
            String md5 = InspectHandles.calcMD5(columns, next);
            listDataMap2.get(i-1).put("MD5", "");
            if (emptyMd5.equals(md5)) {
                duplicationMess.append("第").append(index).append("行数据为空行").append("\r\n");
                duplicationCount++;
                it.remove();

                listDataMap2.get(i-1).put("errorMess", "数据为空");
                listDataMap2.get(i-1).put("idDel", "1");
                continue;
            }
            // excel中数据重复处理
            if (set.contains(md5)) {
                // excel中重复的数据不入库，直接剔除
                duplicationMess.append("第").append(index).append("行重复数据，已删除").append("\r\n");
                duplicationCount++;
                it.remove();
                listDataMap2.get(i-1).put("errorMess", "重复数据");
                listDataMap2.get(i-1).put("idDel", "1");
                continue;
            }
            set.add(md5);
            next.put("MD5", md5);
            listDataMap2.get(i-1).put("MD5", md5);

            if(checkRepeat){
                // 根据MD5从数据库查询数据
                int num = ruleCheckingDao.repeatCheck(md5, "t_" + table.getResourceId());
                if (num > 0) {
                    // 和数据库重复的数据不入库，直接剔除
                    duplicationMess.append("第").append(index).append("行重复数据，已删除").append("\r\n");
                    duplicationCount++;
                    it.remove();
                    listDataMap2.get(i-1).put("errorMess", "重复数据");
                    listDataMap2.get(i-1).put("idDel", "1");
                    continue;
                }
            }


            // excel数据和数据库数据重复处理
            if (checkRepeat && !useMemory && !("dc_focus_company_new").equals(table.getResourceId())) {
            	
            	
            	// 根据MD5从数据库查询数据
                int num = ruleCheckingDao.repeatCheck(md5, "t_" + table.getResourceId());
                if (num > 0) {
                    // 和数据库重复的数据不入库，直接剔除
                    duplicationMess.append("第").append(index).append("行重复数据，已删除").append("\r\n");
                    duplicationCount++;
                    it.remove();
                    listDataMap2.get(i-1).put("errorMess", "重复数据");
                    listDataMap2.get(i-1).put("idDel", "1");
                }
            }
        }
        Map<String, Object> map = new HashMap<>(3);
        map.put("duplicationMess", StringUtils.defaultIfBlank(duplicationMess.toString(), ""));
        map.put("duplicationCount", duplicationCount);
        map.put("listDataMap", listDataMap);
        map.put("listDataMap2", listDataMap2);
        return map;
    }
    /**
     * 重复性校验：先生成MD5,然后删除
     *
     * @param listDataMap 数据的map
     * @param table       要上传的表
     * @param columns     上传字段
     * @param useMemory   是否使用临时表 true是 false否
     * @return 校验的结果
     */
    @Override
    public Map<String, Object> duplicationCheck_new(List<Map<String, Object>> listDataMap, Table table,
                                                List<Column> columns, boolean useMemory, boolean checkRepeat, Sheet st1) {
        StringBuilder duplicationMess = new StringBuilder(200);
        int duplicationCount = 0;
        int i = 0;
        Set<String> set = new HashSet<>(listDataMap.size());
        List<Map<String, Object>> listDataMap2 = (List<Map<String, Object>>) JSONArray.parse(JSONArray.toJSONString(listDataMap));
        for(Map<String, Object> data:listDataMap2){
            data.put("errorMess","");
        }
        String emptyMd5 = InspectHandles.calcMD5byColumn(columns);
        for (Iterator<Map<String, Object>> it = listDataMap.listIterator(); it.hasNext(); ) {
            Map<String, Object> next = it.next();
            i++;
            String index = (null != next.get("rowIndex") ? String.valueOf(next.get("rowIndex")) : String.valueOf(i));
            //MD5计算
            String md5 = InspectHandles.calcMD5(columns, next);
//            listDataMap2.get(i-1).put("MD5", "");
            if (emptyMd5.equals(md5)) {
                duplicationMess.append("第").append(index).append("行数据为空行").append("\r\n");
                duplicationCount++;
                it.remove();

                HSSFRow row1= (HSSFRow) st1.getRow(Integer.parseInt(index)-1);  //
                // 创建单元格（Cell）
                Cell cell = row1.createCell(row1.getLastCellNum());
                // 设置单元格内容
                cell.setCellValue("数据为空");


                listDataMap2.get(Integer.parseInt(index)-2).put("errorMess", "数据为空");
                listDataMap2.get(Integer.parseInt(index)-2).put("idDel", "1");
                continue;
            }
            // excel中数据重复处理
            if (set.contains(md5)) {
                // excel中重复的数据不入库，直接剔除
                duplicationMess.append("第").append(index).append("行重复数据，已删除").append("\r\n");
                duplicationCount++;
                it.remove();
                HSSFRow row1= (HSSFRow) st1.getRow(Integer.parseInt(index)-1);  //
                // 创建单元格（Cell）
                Cell cell = row1.createCell(row1.getLastCellNum());
                // 设置单元格内容
                cell.setCellValue("上传失败，请在“错误数据处理”模块中删除原有错误数据后，再次进行导入。");
                listDataMap2.get(Integer.parseInt(index)-2).put("errorMess", "上传失败，请在“错误数据处理”模块中删除原有错误数据后，再次进行导入。");
                listDataMap2.get(Integer.parseInt(index)-2).put("idDel", "1");
                continue;
            }
            set.add(md5);
            next.put("MD5", md5);
//            listDataMap2.get(i-1).put("MD5", md5);

            if(checkRepeat){
                // 根据MD5从数据库查询数据
                int num = ruleCheckingDao.repeatCheck(md5, "t_" + table.getResourceId());
                if (num > 0) {
                    // 和数据库重复的数据不入库，直接剔除
                    duplicationMess.append("第").append(index).append("行重复数据，已删除").append("\r\n");
                    duplicationCount++;
                    it.remove();
                    HSSFRow row1= (HSSFRow) st1.getRow(Integer.parseInt(index)-1);  //
                    // 创建单元格（Cell）
                    Cell cell = row1.createCell(row1.getLastCellNum());
                    // 设置单元格内容
                    cell.setCellValue("上传失败，请在“错误数据处理”模块中删除原有错误数据后，再次进行导入。");
                    listDataMap2.get(Integer.parseInt(index)-2).put("errorMess", "上传失败，请在“错误数据处理”模块中删除原有错误数据后，再次进行导入。");
                    listDataMap2.get(Integer.parseInt(index)-2).put("idDel", "1");
                }
            }


            // excel数据和数据库数据重复处理
//            if (checkRepeat && !useMemory && !("dc_focus_company_new").equals(table.getResourceId())) {
//
//
//            	// 根据MD5从数据库查询数据
//                int num = ruleCheckingDao.repeatCheck(md5, "t_" + table.getResourceId());
//                if (num > 0) {
//                    // 和数据库重复的数据不入库，直接剔除
//                    duplicationMess.append("第").append(index).append("行重复数据，已删除").append("\r\n");
//                    duplicationCount++;
//                    it.remove();
//                    HSSFRow row1= (HSSFRow) st1.getRow(Integer.parseInt(index));  //
//                    // 创建单元格（Cell）
//                    Cell cell = row1.createCell(row1.getLastCellNum());
//                    // 设置单元格内容
//                    cell.setCellValue("重复数据");
//                    listDataMap2.get(i-1).put("errorMess", "重复数据");
//                    listDataMap2.get(i-1).put("idDel", "1");
//                }
//            }
        }
        Map<String, Object> map = new HashMap<>(3);
        map.put("duplicationMess", StringUtils.defaultIfBlank(duplicationMess.toString(), ""));
        map.put("duplicationCount", duplicationCount);
        map.put("listDataMap", listDataMap);
        map.put("listDataMap2", listDataMap2);
        return map;
    }

    /**
     * 功能描述:单条数据数据库重复性校验
     *
     * @param dataMap 待校验数据
     * @param table   资源信息
     * @param columns 待校验字段
     * @return 校验结果
     * @author YaoS
     * @date 19/3/29 17:00
     */
    private String dbDuplicationCheck(Map<String, Object> dataMap, Table table, List<Column> columns) {
        String msg = null;
        String emptyMd5 = InspectHandles.calcMD5byColumn(columns);
        if (!("dc_focus_company_new").equals(table.getResourceId())) {
            //MD5计算
            String md5 = InspectHandles.calcMD5(columns, dataMap);
            if (emptyMd5.equals(md5)) {
                msg = "数据为空";
                return msg;
            }
            // 根据MD5从数据库查询数据
            int num = ruleCheckingDao.repeatCheck(md5, "t_" + table.getResourceId());
            if (num > 0) {
                msg = "数据重复";
            }
        }
        return msg;
    }

    @Override
    public Map<String, Object> styleCheck(BatchLog batchLog, List<Map<String, Object>> listDataMap, Table table,
                                          List<Column> columns, List<Map<String, Object>> listDataMap2) {
        StringBuilder errorMess = new StringBuilder(200);
        Map<String, String> errorCol;
        String tableMsg = null;
        int errorCount = 0;
        String index = "1";
        int i = 0;
        Map<String, Object> map = new HashMap<>(4);
        // 获取当前版本
        int editionId = ruleCheckService.selectCurrentContentVersion();

        Map<String, Object> tableType = ruleCheckingDao.selectTableType(table.getTableId());
        // 获取表规则
        List<InspectRule> rules = ruleCheckingDao.selctTableRules(table.getTableId(), editionId);
        List<InspectRule> rules2 = null;
        if(tableType !=null&&tableType.get("table_id2") !=null && !"".equals(tableType.get("table_id2"))){
            rules2 = ruleCheckingDao.selctTableRules(Integer.parseInt(tableType.get("table_id2").toString()), editionId);
        }

        System.out.println(table.getTableId()+"--"+editionId+"ruleCheckingDao");
        // 字段规则Map
        Map<String, List<InspectRule>> columnRules = new HashMap<>(columns.size());

        Map<String, List<InspectRule>> columnRules2 = new HashMap<>(columns.size());
        // 获取所有字段规则并填充Map
        for (Column c : columns) {
            List<InspectRule> list = ruleCheckingDao.selectColumnRule(c.getMetricCode(), editionId, table.getTableId());
            if (!CollectionUtils.isEmpty(list)) {
                columnRules.put(c.getMetricCode(), list);
            }
            if(tableType !=null&&tableType.get("table_id2") !=null && !"".equals(tableType.get("table_id2"))){
                List<InspectRule> list2 = ruleCheckingDao.selectColumnRule(c.getMetricCode(), editionId, Integer.parseInt(tableType.get("table_id2").toString()));
                if (!CollectionUtils.isEmpty(list)) {
                    columnRules2.put(c.getMetricCode(), list2);
                }
            }
        }
        List<JSONObject> jsonlist = new ArrayList<>();
        for (Iterator<Map<String, Object>> it = listDataMap.listIterator(); it.hasNext(); ) {
            i++;
            Map<String, Object> next = it.next();                        
            index = (null != next.get("rowIndex") ? String.valueOf(next.get("rowIndex")) : String.valueOf(i));

            if(tableType.get("xdrlbcol")!=null &&!"".equals(tableType.get("xdrlbcol"))
                    &&"自然人".equals(next.get(tableType.get("xdrlbcol").toString()))
                    &&columnRules2.size()>0){
                // 字段规则校验
                errorCol = this.colunmCheck(batchLog, next, table, columns, index, columnRules2);
                JSONObject json = new JSONObject();
                String colMsg = errorCol.get("errMsg");
                String dataId = errorCol.get("dataId");
                // 表规则校验
                if (!CollectionUtils.isEmpty(rules2)) {
                    if (StringUtils.isNotEmpty(dataId)) {
                        tableMsg = this.errDataTableCheck(batchLog, next, table, columns, Integer.parseInt(dataId), index,
                                rules2);
                    } else {
                        tableMsg = this.dataTableCheck(batchLog, next, table, columns, index, rules2);
                    }
                }
                if(next.containsKey("taskId")) {
                    json.put("taskId", next.get("taskId"));
                    json.put("errmess", colMsg+tableMsg);
                }
                // 整条数据规则校验结果
                if (StringUtils.isNotBlank(colMsg) || StringUtils.isNotBlank(tableMsg)) {
                    errorMess.append(StringUtils.isEmpty(colMsg) ? "" : colMsg);
                    errorMess.append(StringUtils.isEmpty(tableMsg) ? "" : tableMsg);
                    errorCount++;
                    jsonlist.add(json);
                    listDataMap2.get(Integer.parseInt(index)-1).put("errorMess", errorMess.toString());
                    listDataMap2.get(Integer.parseInt(index)-1).put("idDel", "1");
                    it.remove();
                }
            }else{
                // 字段规则校验
                errorCol = this.colunmCheck(batchLog, next, table, columns, index, columnRules);
                JSONObject json = new JSONObject();
                String colMsg = errorCol.get("errMsg");
                String dataId = errorCol.get("dataId");
                // 表规则校验
                if (!CollectionUtils.isEmpty(rules)) {
                    if (StringUtils.isNotEmpty(dataId)) {
                        tableMsg = this.errDataTableCheck(batchLog, next, table, columns, Integer.parseInt(dataId), index,
                                rules);
                    } else {
                        tableMsg = this.dataTableCheck(batchLog, next, table, columns, index, rules);
                    }
                }
                if(next.containsKey("taskId")) {
                    json.put("taskId", next.get("taskId"));
                    json.put("errmess", colMsg+tableMsg);
                }
                // 整条数据规则校验结果
                if (StringUtils.isNotBlank(colMsg) || StringUtils.isNotBlank(tableMsg)) {
                    errorMess.append(StringUtils.isEmpty(colMsg) ? "" : colMsg);
                    errorMess.append(StringUtils.isEmpty(tableMsg) ? "" : tableMsg);
                    errorCount++;
                    jsonlist.add(json);
                    listDataMap2.get(Integer.parseInt(index)-1).put("errorMess", errorMess.toString());
                    listDataMap2.get(Integer.parseInt(index)-1).put("idDel", "1");
                    it.remove();
                }
            }
//            next.get();

        }
        map.put("mess", StringUtils.defaultIfBlank(errorMess.toString(), ""));
        map.put("errorCount", errorCount);
        map.put("errlist", jsonlist);
        map.put("listDataMap", listDataMap);
        return map;
    }
    @Override
    public Map<String, Object> styleCheck_new(BatchLog batchLog, List<Map<String, Object>> listDataMap, Table table,
                                          List<Column> columns, List<Map<String, Object>> listDataMap2,Sheet st1) {
        StringBuilder errorMess = new StringBuilder(200);
        Map<String, String> errorCol;
        String tableMsg = null;
        int errorCount = 0;
        String index = "1";
        int i = 0;
        Map<String, Object> map = new HashMap<>(4);
        // 获取当前版本
        int editionId = ruleCheckService.selectCurrentContentVersion();

        Map<String, Object> tableType = ruleCheckingDao.selectTableType(table.getTableId());
        // 获取表规则
        List<InspectRule> rules = ruleCheckingDao.selctTableRules(table.getTableId(), editionId);
        List<InspectRule> rules2 = null;
        if(tableType !=null&&tableType.get("table_id2") !=null && !"".equals(tableType.get("table_id2"))){
            rules2 = ruleCheckingDao.selctTableRules(Integer.parseInt(tableType.get("table_id2").toString()), editionId);
        }

        System.out.println(table.getTableId()+"--"+editionId+"ruleCheckingDao");
        // 字段规则Map
        Map<String, List<InspectRule>> columnRules = new HashMap<>(columns.size());

        Map<String, List<InspectRule>> columnRules2 = new HashMap<>(columns.size());
        // 获取所有字段规则并填充Map
        for (Column c : columns) {
            List<InspectRule> list = ruleCheckingDao.selectColumnRule(c.getMetricCode(), editionId, table.getTableId());
            if (!CollectionUtils.isEmpty(list)) {
                columnRules.put(c.getMetricCode(), list);
            }
            if(tableType !=null&&tableType.get("table_id2") !=null && !"".equals(tableType.get("table_id2"))){
                List<InspectRule> list2 = ruleCheckingDao.selectColumnRule(c.getMetricCode(), editionId, Integer.parseInt(tableType.get("table_id2").toString()));
                if (!CollectionUtils.isEmpty(list)) {
                    columnRules2.put(c.getMetricCode(), list2);
                }
            }
        }
        List<JSONObject> jsonlist = new ArrayList<>();
        for (Iterator<Map<String, Object>> it = listDataMap.listIterator(); it.hasNext(); ) {
            i++;
            Map<String, Object> next = it.next();
            index = (null != next.get("rowIndex") ? String.valueOf(next.get("rowIndex")) : String.valueOf(i));

            if(tableType.get("xdrlbcol")!=null &&!"".equals(tableType.get("xdrlbcol"))
                    &&"自然人".equals(next.get(tableType.get("xdrlbcol").toString()))
                    &&columnRules2.size()>0){
                // 字段规则校验
                errorCol = this.colunmCheck(batchLog, next, table, columns, index, columnRules2);
                JSONObject json = new JSONObject();
                String colMsg = errorCol.get("errMsg");
                String dataId = errorCol.get("dataId");
                // 表规则校验
                if (!CollectionUtils.isEmpty(rules2)) {
                    if (StringUtils.isNotEmpty(dataId)) {
                        tableMsg = this.errDataTableCheck(batchLog, next, table, columns, Integer.parseInt(dataId), index,
                                rules2);
                    } else {
                        tableMsg = this.dataTableCheck(batchLog, next, table, columns, index, rules2);
                    }
                }
                if(next.containsKey("taskId")) {
                    json.put("taskId", next.get("taskId"));
                    json.put("errmess", colMsg+tableMsg);
                }
                // 整条数据规则校验结果
                if (StringUtils.isNotBlank(colMsg) || StringUtils.isNotBlank(tableMsg)) {
                    errorMess.append(StringUtils.isEmpty(colMsg) ? "" : colMsg);
                    errorMess.append(StringUtils.isEmpty(tableMsg) ? "" : tableMsg);
                    errorCount++;
                    jsonlist.add(json);
//                    listDataMap2.get(Integer.parseInt(index)-1).put("errorMess", errorMess.toString());
//                    listDataMap2.get(Integer.parseInt(index)-1).put("idDel", "1");
                    it.remove();
                }
            }else{
                // 字段规则校验
                errorCol = this.colunmCheck(batchLog, next, table, columns, index, columnRules);
                JSONObject json = new JSONObject();
                String colMsg = errorCol.get("errMsg");
                String dataId = errorCol.get("dataId");
                // 表规则校验
                if (!CollectionUtils.isEmpty(rules)) {
                    if (StringUtils.isNotEmpty(dataId)) {
                        tableMsg = this.errDataTableCheck(batchLog, next, table, columns, Integer.parseInt(dataId), index,
                                rules);
                    } else {
                        tableMsg = this.dataTableCheck(batchLog, next, table, columns, index, rules);
                    }
                }
                if(next.containsKey("taskId")) {
                    json.put("taskId", next.get("taskId"));
                    json.put("errmess", colMsg+tableMsg);
                }
                // 整条数据规则校验结果
                if (StringUtils.isNotBlank(colMsg) || StringUtils.isNotBlank(tableMsg)) {
                    errorMess.append(StringUtils.isEmpty(colMsg) ? "" : colMsg);
                    errorMess.append(StringUtils.isEmpty(tableMsg) ? "" : tableMsg);
                    errorCount++;
                    jsonlist.add(json);
//                    listDataMap2.get(Integer.parseInt(index)-1).put("errorMess", errorMess.toString());
//                    listDataMap2.get(Integer.parseInt(index)-1).put("idDel", "1");
                    it.remove();
                }
            }
//            next.get();
            if(!"".equals(StringUtils.defaultIfBlank(errorMess.toString(), ""))){
                listDataMap2.get(Integer.parseInt(index)-2).put("errorMess", StringUtils.defaultIfBlank(errorMess.toString(), ""));
                listDataMap2.get(Integer.parseInt(index)-2).put("idDel", "1");
                HSSFRow row1= (HSSFRow) st1.getRow(Integer.parseInt(index)-1);  //
                // 创建单元格（Cell）
                Cell cell = row1.createCell(row1.getLastCellNum()-1);
                // 设置单元格内容
                cell.setCellValue(StringUtils.defaultIfBlank(errorMess.toString(), ""));
                errorMess.setLength(0);
            }
        }
        map.put("mess", StringUtils.defaultIfBlank(errorMess.toString(), ""));
        map.put("errorCount", errorCount);
        map.put("errlist", jsonlist);
        map.put("listDataMap", listDataMap);
        return map;
    }

    /**
     * 功能描述: 对首次入库数据字段规则校验未通过的进行表规则校验
     *
     * @param batchLog 批次信息
     * @param map      待校验数据
     * @param table    资源信息
     * @param columns  字段信息
     * @param dataId   数据id
     * @return 校验结果
     * @author YaoS
     * @date 19/3/29 14:36
     */
    private String errDataTableCheck(BatchLog batchLog, Map<String, Object> map, Table table, List<Column> columns,
                                     int dataId, String index, List<InspectRule> rules) {
        StringBuilder sb = new StringBuilder();
        // 整条数据表规则校验 默认校验通过
        boolean totalValidate = true;
        for (InspectRule isr : rules) {
            boolean validate = true;
            try {
            	map.put("tableId", table.getTableId());
            	map.put("rootId", table.getRootId());
                validate = InspectHandles.ruleCheckForTable(isr, map);
            } catch (Exception e) {
                ExceptionLogger.error("InspectDataStyleServiceImpl#errorDataTableCheck",
                        "错误数据校验异常！ERROR:" + e.getMessage(), e);
            }
            if (!validate) {
                // 一条规则未通过就是未通过
                totalValidate = false;
                sb.append(table.getResourceTitle()).append(":").append(isr.getRuleDesc()).append(";");
            }
        }
        if (!totalValidate) {
            // 表规则异常数据入库
            ruleCheckingDao.addErrorData(null, null, null, table.getTableId(), batchLog.getManagerId(),
                    batchLog.getDeptId(), "该记录不符合表规则（" + sb.toString() + "）", dataId, "E", "B");
        }
        String errorMsg = sb.toString();
        if (StringUtils.isNotBlank(errorMsg)) {
            errorMsg = "第" + index + "行数据表规则校验未通过(" + errorMsg + ")\r\n";
        }
        return errorMsg;
    }

    /**
     * 功能描述: 表规则校验
     *
     * @param batchLog 批次信息
     * @param map      待校验数据
     * @param table    资源信息
     * @param columns  资源字段信息
     * @param index    待校验数据行号
     * @param rules    表规则list
     * @return 校验结果
     * @date 19/3/29 14:26
     */
    private String dataTableCheck(BatchLog batchLog, Map<String, Object> map, Table table, List<Column> columns,
                                  String index, List<InspectRule> rules) {
        StringBuilder sb = new StringBuilder();
        // 所有表规则校验结果 默认校验通过
        boolean totalValidate = true;
        for (InspectRule isr : rules) {
            //单条规则校验结果，默认校验通过
            boolean validate = true;
            try {
            	map.put("tableId", table.getTableId());
            	map.put("rootId", table.getRootId());
                //对数据进行校验
                validate = InspectHandles.ruleCheckForTable(isr, map);
            } catch (Exception e) {
                ExceptionLogger.error("InspectDataStyleServiceImpl#tableCheck", "表规则校验异常！ERROR:" + e.getMessage(), e);
            }
            // 校验未通过
            if (!validate) {
                // 一条规则未通过就是未通过
                totalValidate = false;
                sb.append(table.getResourceTitle()).append(":").append(isr.getRuleDesc()).append(";");
            }
        }
        // 如数据的表规则未通过,则将错误数据入库
        if (!totalValidate) {
            StringBuilder sql = new StringBuilder(200);
            sql.append(" insert into t_" + table.getResourceId());
            sql.append(" ( tybm, ");
            for (int i = 0; i < columns.size(); i++) {
                sql.append(columns.get(i).getMetricCode());
                sql.append(" , ");
            }
            sql.append("status, md5, batch_id ) ");
            sql.append(" VALUES ( ");
            sql.append(InspectHandles.SINGLE_QUOTATION).append(InspectHandles.SINGLE_QUOTATION).append(", ");
            for (Column column : columns) {
                String value;
                if (null != map.get(column.getMetricCode())) {
                    value = String.valueOf(map.get(column.getMetricCode()));
                } else {
                    value = null;
                }
                if ("number".equals(column.getFieldType())) {
                    sql.append(StringUtils.isEmpty(value) ? null : value).append(" , ");
                } else if ("timestamp".equals(column.getFieldType())) {
                    sql.append(StringUtils.isEmpty(value) ? null : "\'" + value + "\'").append(" , ");
                } else {
                    sql.append(StringUtils.isEmpty(value) ? null : "\'" + value + "\'").append(" , ");
                }
            }
            // 规则验证错误
            sql.append(InspectHandles.SINGLE_QUOTATION).append("E").append(InspectHandles.SINGLE_QUOTATION).append(", ");
            sql.append(InspectHandles.SINGLE_QUOTATION).append((map.get("MD5"))).append(InspectHandles.SINGLE_QUOTATION).append(", ");
            sql.append(InspectHandles.SINGLE_QUOTATION).append(batchLog.getId()).append(InspectHandles.SINGLE_QUOTATION);
            sql.append(" )");
            // 添加规则验证错误的数据
            DynamicSqlEntity entity = new DynamicSqlEntity(sql.toString());
            ruleCheckingDao.addRepeatData(entity);
            // 表规则异常入库
            ruleCheckingDao.addErrorData(null, null, null, table.getTableId(), batchLog.getManagerId(),
                    batchLog.getDeptId(), "该记录不符合表规则（" + sb.toString() + "）", entity.getId(), "E", "B");
        }
        String errorMsg = sb.toString();
        if (StringUtils.isNotBlank(errorMsg)) {
            errorMsg = "第" + index + "行数据表规则校验未通过(" + errorMsg + ")\r\n";
        }
        return errorMsg;
    }

    /**
     * 功能描述: 字段规则数据校验
     *
     * @param batchLog 批次信息
     * @param map      代校验数据
     * @param table    资源信息
     * @param columns  字段信息
     * @param index    数据指针
     * @return 校验结果
     * @author YaoS
     * @date 19/4/3 10:48
     */
    private Map<String, String> colunmCheck(BatchLog batchLog, Map<String, Object> map, Table table,
                                            List<Column> columns, String index, Map<String, List<InspectRule>> columnRules) {
        Map<String, String> result = new HashMap<>(2);
        // 整条数据的所有字段异常返回信息
        StringBuilder sb = new StringBuilder(200);
        String dataValue = null;
        // 整条数据所有的字段校验结果 true为通过
        boolean totalValidate = true;
        // 字段规则校验异常列名和原因集合
        List<ErrorData> metricList = new ArrayList<>();
        //todo 临时设计，双公示应当放到定制规则校验里
        if (210003 == table.getTableId() || 210001 == table.getTableId()) {
            String extraCflb = map.get("CF_CFLB") == null ? "" : map.get("CF_CFLB").toString();
            String extraQtlb = map.get("CF_QTLB") == null ? "" : map.get("CF_QTLB").toString();
 //           if (extraCflb.indexOf("其他") > -1 && StringUtils.isBlank(extraQtlb)) {
//                totalValidate = false;
//                String reason = "处罚类别中包含【其他】时，其他类别（内容）字段必填;";
//                // 拼接到所有异常
//                sb.append(reason);
//                ErrorData errorData = new ErrorData();
//                errorData.setColunmEN("CF_CFLB");
//                errorData.setColunmCN("处罚类别");
//                errorData.setColunmValue(dataValue);
//                errorData.setReason(reason);
//                errorData.setDeptId(batchLog.getDeptId());
//                errorData.setManagerId(batchLog.getManagerId());
//                errorData.setStatus("E");
//                errorData.setTableId(table.getTableId());
//                errorData.setType("Z");
//                metricList.add(errorData);
//            }
        }

        if (210000 == table.getTableId() || 210002 == table.getTableId()) {
            String extraXklb = map.get("XK_XKLB") == null ? "" : map.get("XK_XKLB").toString();
            String extraQtlb = map.get("XK_QTLB") == null ? "" : map.get("XK_QTLB").toString();
//            if (extraXklb.indexOf("其他") > -1 && StringUtils.isBlank(extraQtlb)) {
//                totalValidate = false;
//                String reason = "许可类别中包含【其他】时，其他类别字段必填;";
//                // 拼接到所有异常
//                sb.append(reason);
//                ErrorData errorData = new ErrorData();
//                errorData.setColunmEN("XK_XKLB");
//                errorData.setColunmCN("许可类别");
//                errorData.setColunmValue(dataValue);
//                errorData.setReason(reason);
//                errorData.setDeptId(batchLog.getDeptId());
//                errorData.setManagerId(batchLog.getManagerId());
//                errorData.setStatus("E");
//                errorData.setTableId(table.getTableId());
//                errorData.setType("Z");
//                metricList.add(errorData);
//            }
        }

        String lx = "";
        String hm = "";
        if (210000 == table.getTableId()) {
            lx = map.get("XK_FDDBRZJLX") == null ? "" : map.get("XK_FDDBRZJLX").toString();
            hm = map.get("XK_FDDBRZJHM") == null ? "" : map.get("XK_FDDBRZJHM").toString();
        } else if (210001 == table.getTableId()) {
            lx = map.get("CF_FDDBRZJLX") == null ? "" : map.get("CF_FDDBRZJLX").toString();
            hm = map.get("CF_FDDBRZJHM") == null ? "" : map.get("CF_FDDBRZJHM").toString();
        } else if (210002 == table.getTableId()) {
            lx = map.get("XK_XDR_ZJLX") == null ? "" : map.get("XK_XDR_ZJLX").toString();
            hm = map.get("B000002") == null ? "" : map.get("B000002").toString();
        } else if (210003 == table.getTableId()) {
            lx = map.get("CF_XDR_ZJLX") == null ? "" : map.get("CF_XDR_ZJLX").toString();
            hm = map.get("B000002") == null ? "" : map.get("B000002").toString();
        }
        if (StringUtils.isNotBlank(lx) && StringUtils.isBlank(hm)) {
            totalValidate = false;
            String reason = "证件类型不为空，证件号码必填;";
            // 拼接到所有异常
            sb.append(reason);
            ErrorData errorData = new ErrorData();
            if (210000 == table.getTableId()) {
                errorData.setColunmEN("XK_FDDBRZJHM");
                errorData.setColunmCN("证件号码");
            } else if (210001 == table.getTableId()) {
                errorData.setColunmEN("CF_FDDBRZJHM");
                errorData.setColunmCN("证件号码");
            } else if (210002 == table.getTableId()) {
                errorData.setColunmEN("B000002");
                errorData.setColunmCN("证件号码");
            } else if (210003 == table.getTableId()) {
                errorData.setColunmEN("B000002");
                errorData.setColunmCN("证件号码");
            }
            errorData.setColunmValue(dataValue);
            errorData.setReason(reason);
            errorData.setDeptId(batchLog.getDeptId());
            errorData.setManagerId(batchLog.getManagerId());
            errorData.setStatus("E");
            errorData.setTableId(table.getTableId());
            errorData.setType("Z");
            metricList.add(errorData);
        }
        //对字段循环，进行类型校验
        for (Column metric : columns) {
            dataValue = InspectHandles.dateToStr(metric, map);
            // 获取字段绑定的规则信息
            List<InspectRule> list = columnRules.get(metric.getMetricCode());
            if (!CollectionUtils.isEmpty(list)) {
                for (InspectRule rule : list) {
                    // 默认校验通过
                    boolean validate = true;
                    try {
                        //字段类型校验
                        validate = InspectHandles.ruleCheckForColumn(rule, dataValue);
                    } catch (Exception e) {
                        ExceptionLogger.error("InspectDataStyleServiceImpl#colunmCheck",
                                "字段规则校验异常！ERROR：" + e.getMessage(), e);
                    }
                    // 校验未通过
                    if (!validate) {
                        // 单个字段校验未通过就为未通过
                        totalValidate = false;
                        String reason = metric.getMetricName() + ":" + rule.getRuleDesc() + ";";
                        // 拼接到所有异常
                        sb.append(reason);
                        ErrorData errorData = new ErrorData();
                        errorData.setColunmEN(metric.getMetricCode());
                        errorData.setColunmCN(metric.getMetricName());
                        errorData.setColunmValue(dataValue);
                        errorData.setReason(reason);
                        errorData.setDeptId(batchLog.getDeptId());
                        errorData.setManagerId(batchLog.getManagerId());
                        errorData.setStatus("E");
                        errorData.setTableId(table.getTableId());
                        errorData.setType("Z");
                        metricList.add(errorData);
                    }
                }
            }
        }
        // 这条数据字段规则校验未通过
        if (!totalValidate) {
            StringBuilder sql = new StringBuilder(200);
            sql.append(" insert into t_" + table.getResourceId());
            sql.append(" (  tybm, ");
            for (int i = 0; i < columns.size(); i++) {
                sql.append(columns.get(i).getMetricCode());
                sql.append(" , ");
            }
            sql.append("status, md5, batch_id ) ");
            sql.append(" VALUES ");

            StringBuilder dataSql = new StringBuilder();
            dataSql.append(" ( ").append(InspectHandles.SINGLE_QUOTATION).append(InspectHandles.SINGLE_QUOTATION).append(", ");

            for (Column column : columns) {
                String value;
                if (null != map.get(column.getMetricCode())) {
                    value = String.valueOf(map.get(column.getMetricCode()));
                } else {
                    value = null;
                }
                if ("number".equals(column.getFieldType())) {
                    dataSql.append(StringUtils.isEmpty(value) ? null : value).append(" , ");
                } else if ("timestamp".equals(column.getFieldType())) {
                    dataSql.append(StringUtils.isEmpty(value) ? null : InspectHandles.SINGLE_QUOTATION + value + InspectHandles.SINGLE_QUOTATION)
                            .append(" , ");
                } else {
                    dataSql.append(StringUtils.isEmpty(value) ? null : InspectHandles.SINGLE_QUOTATION + value + InspectHandles.SINGLE_QUOTATION)
                            .append(" , ");
                }
            }
            // 规则验证错误
            dataSql.append(InspectHandles.SINGLE_QUOTATION).append("E").append(InspectHandles.SINGLE_QUOTATION).append(", ");
            dataSql.append(InspectHandles.SINGLE_QUOTATION).append((map.get("MD5"))).append(InspectHandles.SINGLE_QUOTATION)
                    .append(", ");
            dataSql.append(InspectHandles.SINGLE_QUOTATION).append(batchLog.getId()).append("\')");
            // 添加规则验证错误的数据进入数据表
            DynamicSqlEntity entity = new DynamicSqlEntity(sql.toString() + dataSql.toString());
            ruleCheckingDao.addRepeatData(entity);
            if (!CollectionUtils.isEmpty(metricList)) {
                for (ErrorData e : metricList) {
                    e.setDataId(entity.getId());
                }
                // 将错误字段记录至错误数据表中
                ruleCheckingDao.batchAddErrorData(metricList);
            }
            String errorMsg = sb.toString();
            if (StringUtils.isNotBlank(errorMsg)) {
                errorMsg = "第" + index + "条数据字段规则校验未通过(" + errorMsg + ")\r\n";
            }
            result.put("dataId", String.valueOf(entity.getId()));
            result.put("errMsg", errorMsg);
        }
        return result;
    }

    /**
     * 功能描述: 异常数据修改后再次校验
     *
     * @param tableId 资源ID
     * @param map     待校验数据
     * @param columns 待校验字段
     * @return 校验结果
     * @author Min DongXv
     * @date 18/7/18 11:35
     */
    @Override
    public String dataCheck(int tableId, Map<String, Object> map, List<Column> columns) {
        // 获取数据表信息
        Table tableInfo = tableService.queryCubeById(tableId);
        if(!StringUtils.isNotBlank(String.valueOf(map.get("md5")))) {
        	 // 重复性校验
            String duplication = dbDuplicationCheck(map, tableInfo, columns);
            if (StringUtils.isNotBlank(duplication)) {
                return duplication;
            }
        }
       
        // 数据库规则校验
        map.put("tableId", tableId);
        map.put("rootId", tableInfo.getRootId());
        String dbCheck = InspectHandles.singleDataFormatCheck(map, columns, data -> data.replace("/", "-"),tableId);
        if (StringUtils.isNotBlank(dbCheck)) {
            return dbCheck;
        }
        // 数据规则校验
        return ruleCheckService.ruleCheck(tableId, map, columns);
    }

        /**
     * 数据库规则校验
     *
     * @param batchLog    批次信息
     * @param listDataMap 待校验数据
     * @param table       表信息
     * @param columns     列信息
     * @return 规则校验结果
     */
    @Override
    public Map<String, Object> dataFormatCheck(BatchLog batchLog, List<Map<String, Object>> listDataMap, Table table,
                                               List<Column> columns, List<Map<String, Object>> listDataMap2) {
        int count = 0;
        StringBuilder errMsg = new StringBuilder();
        Map<String, Object> dataFormatError = new HashMap<>(2);
        String index = "1";
        int seq = 0;
        for (Iterator<Map<String, Object>> it = listDataMap.iterator(); it.hasNext(); ) {
            seq++;
            //第一步的数据重复校验已判空，不需要进行非空校验
            Map<String, Object> val = it.next();

         	
            // 序列
            index = (null != val.get("rowIndex") ? String.valueOf(val.get("rowIndex")) : String.valueOf(seq));
            // 整条数据的json
            String data = JSON.toJSONString(val);
            StringBuilder error = new StringBuilder();

            for (Column c : columns) {
                String code = c.getMetricCode();
                // 没有值跳过校验
                if (null == val.get(code) || StringUtils.isBlank(String.valueOf(val.get(code)))) {
                    continue;
                }
                String value = String.valueOf(val.get(code));
                String name = c.getMetricName();
                String type = c.getFieldType();
                if (StringUtils.isNotBlank(value)) {
                    if (InspectHandles.TYPE_TIMESTAMP.equals(type)) {
                    	String extend=  "((\\d{2}(([02468][048])|([13579][26]))[\\-]((((0?[13578])|(1[02]))[\\-]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-]((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-]((((0?[13578])|(1[02]))[\\-]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-]((0?[1-9])|(1[0-9])|(2[0-8]))))))";
                        // 时间类型不匹配
                    	Pattern p = Pattern.compile(extend);
                        Matcher m = p.matcher(value.replace("/", "-"));
                        boolean dateFlag = m.matches();
                        if(!dateFlag){
                        	 error.append("字段" + name + "类型不匹配，应为时间类型;");
                        }
                    } else if (InspectHandles.TYPE_NUMBER.equals(type)) {
                        // 数值类型不匹配
                        Integer tableId = table.getTableId();
                        if (210000 == tableId || 210001 == tableId || 210002 == tableId || 210003 == tableId) {
                            Matcher m = InspectHandles.NUMBER_PATTERN_DOUBLE_PUBLICITY.matcher(value);
                            if (!m.matches()) {
                                error.append("字段" + name + "类型不匹配，应为最多六位小数的数值类型;");
                            }
                        }
//                        else {
//                            Matcher m = InspectHandles.NUMBER_PATTERN.matcher(value);
//                            if (!m.matches()) {
//                                error.append("字段" + name + "类型不匹配，应为最多两位小数的数值类型;");
//                            }
//                        }
                    } else {
                        int length = c.getFieldLength();
                        // 字段长度超长
                        if (value.length() > length) {
                            error.append("字段" + name + "长度超过数据表长度;");
                        }
                    }
                }
            }
            // 数据的数据库规则未通过
            if (StringUtils.isNotBlank(error)) {
                StringBuilder sql = new StringBuilder(200);
                sql.append(" insert into t_" + table.getResourceId());
                sql.append(" ( tybm, ");
                for (int i = 0; i < columns.size(); i++) {
                    sql.append(columns.get(i).getMetricCode());
                    sql.append(" , ");
                }
                sql.append("status, md5, batch_id ) ");
                sql.append(" VALUES ( ");
                sql.append(InspectHandles.SINGLE_QUOTATION).append(InspectHandles.SINGLE_QUOTATION).append(" , ");
                for (int i = 0; i < columns.size(); i++) {
                    sql.append(" null , ");
                }
                // 规则验证错误
                sql.append(InspectHandles.SINGLE_QUOTATION).append("E").append(InspectHandles.SINGLE_QUOTATION).append(" , ");
                sql.append(InspectHandles.SINGLE_QUOTATION).append((val.get("MD5"))).append(InspectHandles.SINGLE_QUOTATION)
                        .append(" , ");
                sql.append(InspectHandles.SINGLE_QUOTATION).append(batchLog.getId()).append(InspectHandles.SINGLE_QUOTATION);
                sql.append(" )");
                // 添加规则验证错误的数据
                DynamicSqlEntity entity = new DynamicSqlEntity(sql.toString());
                ruleCheckingDao.addRepeatData(entity);
                count++;
                // 数据库规则异常入库
                ruleCheckingDao.addErrorData(null, null, data, table.getTableId(), batchLog.getManagerId(),
                        batchLog.getDeptId(), error.toString(), entity.getId(), "E", "S");
                // 未通过的删除
                errMsg.append("第" + index + "行数据校验数据库格式未通过(" + error + ")\r\n");
                listDataMap2.get(Integer.parseInt(index)-2).put("errorMess",error.toString());
                listDataMap2.get(Integer.parseInt(index)-2).put("idDel", "1");
                it.remove();
            }
        }
        dataFormatError.put("count", count);
        dataFormatError.put("errMsg", errMsg);
        return dataFormatError;
    }
        /**
     * 数据库规则校验
     *
     * @param batchLog    批次信息
     * @param listDataMap 待校验数据
     * @param table       表信息
     * @param columns     列信息
     * @return 规则校验结果
     */
    @Override
    public Map<String, Object> dataFormatCheck_new(BatchLog batchLog, List<Map<String, Object>> listDataMap, Table table,
                                               List<Column> columns, List<Map<String, Object>> listDataMap2, Sheet st1) {
        int count = 0;
        StringBuilder errMsg = new StringBuilder();
        Map<String, Object> dataFormatError = new HashMap<>(2);
        String index = "1";
        int seq = 0;
        for (Iterator<Map<String, Object>> it = listDataMap.iterator(); it.hasNext(); ) {
            seq++;
            //第一步的数据重复校验已判空，不需要进行非空校验
            Map<String, Object> val = it.next();


            // 序列
            index = (null != val.get("rowIndex") ? String.valueOf(val.get("rowIndex")) : String.valueOf(seq));
            // 整条数据的json
            String data = JSON.toJSONString(val);
            StringBuilder error = new StringBuilder();

            for (Column c : columns) {
                String code = c.getMetricCode();
                // 没有值跳过校验
                if (null == val.get(code) || StringUtils.isBlank(String.valueOf(val.get(code)))) {
                    continue;
                }
                String value = String.valueOf(val.get(code));
                String name = c.getMetricName();
                String type = c.getFieldType();
                if (StringUtils.isNotBlank(value)) {
                    if (InspectHandles.TYPE_TIMESTAMP.equals(type)) {
                    	String extend=  "((\\d{2}(([02468][048])|([13579][26]))[\\-]((((0?[13578])|(1[02]))[\\-]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-]((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-]((((0?[13578])|(1[02]))[\\-]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-]((0?[1-9])|(1[0-9])|(2[0-8]))))))";
                        // 时间类型不匹配
                    	Pattern p = Pattern.compile(extend);
                        Matcher m = p.matcher(value.replace("/", "-"));
                        boolean dateFlag = m.matches();
                        if(!dateFlag){
                        	 error.append("字段" + name + "类型不匹配，应为时间类型;");
                        }
                    } else if (InspectHandles.TYPE_NUMBER.equals(type)) {
                        // 数值类型不匹配
                        Integer tableId = table.getTableId();
                        if (210000 == tableId || 210001 == tableId || 210002 == tableId || 210003 == tableId) {
                            Matcher m = InspectHandles.NUMBER_PATTERN_DOUBLE_PUBLICITY.matcher(value);
                            if (!m.matches()) {
                                error.append("字段" + name + "类型不匹配，应为最多六位小数的数值类型;");
                            }
                        }
//                        else {
//                            Matcher m = InspectHandles.NUMBER_PATTERN.matcher(value);
//                            if (!m.matches()) {
//                                error.append("字段" + name + "类型不匹配，应为最多两位小数的数值类型;");
//                            }
//                        }
                    } else {
                        int length = c.getFieldLength();
                        // 字段长度超长
                        if (value.length() > length) {
                            error.append("字段" + name + "长度超过数据表长度;");
                        }
                    }
                }
            }
            // 数据的数据库规则未通过
            if (StringUtils.isNotBlank(error)) {
                StringBuilder sql = new StringBuilder(200);
                sql.append(" insert into t_" + table.getResourceId());
                sql.append(" ( tybm, ");
                for (int i = 0; i < columns.size(); i++) {
                    sql.append(columns.get(i).getMetricCode());
                    sql.append(" , ");
                }
                sql.append("status, md5, batch_id ) ");
                sql.append(" VALUES ( ");
                sql.append(InspectHandles.SINGLE_QUOTATION).append(InspectHandles.SINGLE_QUOTATION).append(" , ");
                for (int i = 0; i < columns.size(); i++) {
                    sql.append(" null , ");
                }
                // 规则验证错误
                sql.append(InspectHandles.SINGLE_QUOTATION).append("E").append(InspectHandles.SINGLE_QUOTATION).append(" , ");
                sql.append(InspectHandles.SINGLE_QUOTATION).append((val.get("MD5"))).append(InspectHandles.SINGLE_QUOTATION)
                        .append(" , ");
                sql.append(InspectHandles.SINGLE_QUOTATION).append(batchLog.getId()).append(InspectHandles.SINGLE_QUOTATION);
                sql.append(" )");
                // 添加规则验证错误的数据
                DynamicSqlEntity entity = new DynamicSqlEntity(sql.toString());
                ruleCheckingDao.addRepeatData(entity);
                count++;
                // 数据库规则异常入库
                ruleCheckingDao.addErrorData(null, null, data, table.getTableId(), batchLog.getManagerId(),
                        batchLog.getDeptId(), error.toString(), entity.getId(), "E", "S");
                // 未通过的删除
                errMsg.append("第" + index + "行数据校验数据库格式未通过(" + error + ")\r\n");
//                listDataMap2.get(Integer.parseInt(index)-1).put("errorMess",error.toString());
//                listDataMap2.get(Integer.parseInt(index)-1).put("idDel", "1");
                it.remove();
            }
            listDataMap2.get(Integer.parseInt(index)-2).put("errorMess",errMsg.toString());
            listDataMap2.get(Integer.parseInt(index)-2).put("idDel", "1");
            HSSFRow row1= (HSSFRow) st1.getRow(Integer.parseInt(index)-1);  //
            // 创建单元格（Cell）
            Cell cell = row1.createCell(row1.getLastCellNum());
            // 设置单元格内容
            cell.setCellValue(errMsg.toString());
        }
        dataFormatError.put("count", count);
        dataFormatError.put("errMsg", errMsg);
        return dataFormatError;
    }

}
